using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;

namespace FireStarterModeller.UI
{
    public class BusinessObjectInfosGridPanelControl : UserControlWin, IFormControl
    {
        private GridAndBOEditorControl<BusinessObjectInfo> _gridAndBOEditorControl;

        public event EventHandler<BOEventArgs<BusinessObjectInfo>> BusinessObjectInfoSelected;

        public BusinessObjectInfosGridPanelControl(IControlFactory controlFactory)
        {
            _gridAndBOEditorControl = new GridAndBOEditorControl<BusinessObjectInfo>(controlFactory, new BusinessObjectInfoControl(controlFactory));

            //TODO: remove this code. PRoject needs to be created somewhere else.
            ProjectInfo projectInfo;
            BusinessObjectCollection<ProjectInfo> projectInfos = new BusinessObjectCollection<ProjectInfo>();
            projectInfos.LoadAll();
            if (projectInfos.Count == 0)
            {
                projectInfo = new ProjectInfo();
                projectInfo.ProjectName = "Test";
                projectInfo.SolutionDirectory = "SolutionDir";
                projectInfo.Save();
            }
            else
            {
                projectInfo = projectInfos[0];
            }

            BusinessObjectCollection<BusinessObjectInfo> col = projectInfo.BusinessObjects;
            //col.LoadAll();
            _gridAndBOEditorControl.SetBusinessObjectCollection(col);

            IControlHabanero control = _gridAndBOEditorControl;

            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(control, BorderLayoutManager.Position.Centre);

            _gridAndBOEditorControl.ReadOnlyGridControl.Grid.BusinessObjectSelected +=
                (sender, e) => FireBusinessObjectInfoSelected(_gridAndBOEditorControl.CurrentBusinessObject);

        }

        public GridAndBOEditorControl<BusinessObjectInfo> GridAndBOEditorControl
        {
            get { return _gridAndBOEditorControl; }
        }

        public BusinessObjectInfo BusinessObjectInfo
        {
            get { return _gridAndBOEditorControl.CurrentBusinessObject;  }
            set {
                _gridAndBOEditorControl.CurrentBusinessObject = value;
                

            }
        }

        private void FireBusinessObjectInfoSelected(BusinessObjectInfo info)
        {
            if (this.BusinessObjectInfoSelected != null)
            {
                this.BusinessObjectInfoSelected(this, new BOEventArgs<BusinessObjectInfo>(info));
            }
        }

        public void SetForm(IFormHabanero form)
        {
            
        }

    }
}
